﻿using Dapper;
using LingHaoHelperTool.Log;
using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Protocols;
using Newtonsoft.Json;
using System.Data;

namespace linghaoDal
{
    public class DbFunction<T>
    {
        public const string dbConnectionString = "Server=120.55.12.241;Database=LingHao;uid=sa;Password=meihanling2..;TrustServerCertificate=True;";
        public DbFunction() { }

        public async Task<IEnumerable<T>> QueryAsync(string Sql)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    LingHaoLogHelper.LogInfo("QueryAsync方法执行的SQL语句是：" + Sql, true);
                    return await db.QueryAsync<T>(Sql);

                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("QueryAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }

        }
        public async Task<IEnumerable<T>> QueryAsync(string Sql, object? param)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    var msg = "";
                    if (param != null)
                    {
                        msg = "，参数是：" + JsonConvert.SerializeObject(param);
                    }
                    LingHaoLogHelper.LogInfo("QueryAsync方法执行的SQL语句是：" + Sql + msg,true);
                    return await db.QueryAsync<T>(Sql, param);

                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("QueryAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }

        }

        public async Task<T> QueryFirstAsync(string Sql)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    LingHaoLogHelper.LogInfo("QueryFirstAsync方法执行的SQL语句是：" + Sql);
                    return await db.QueryFirstAsync<T>(Sql);

                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("QueryFirstAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }
        }

        public async Task<T> QueryFirstAsync(string Sql, object? param)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    var msg = "";
                    if (param != null)
                    {
                        msg = "，参数是：" + JsonConvert.SerializeObject(param);
                    }
                    LingHaoLogHelper.LogInfo("QueryFirstAsync方法执行的SQL语句是：" + Sql + msg);
                    return await db.QueryFirstAsync<T>(Sql, param);

                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("QueryFirstAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }
        }

        public async Task<int> ExecuteAsync(string Sql)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    LingHaoLogHelper.LogInfo("ExecuteAsync方法执行的SQL语句是：" + Sql);
                    return await db.ExecuteAsync(Sql);
                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("ExecuteAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }
        }

        public async Task<int> ExecuteAsync(string Sql, object? param)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(dbConnectionString))
                {
                    db.Open();
                    var msg = "";
                    if (param != null)
                    {
                        msg = "，参数是：" + JsonConvert.SerializeObject(param);
                    }
                    LingHaoLogHelper.LogInfo("ExecuteAsync方法执行的SQL语句是：" + Sql + msg);
                    return await db.ExecuteAsync(Sql, param);
                }
            }
            catch (Exception ex)
            {
                LingHaoLogHelper.LogError("ExecuteAsync方法，未知异常：" + ex.ToString());
                throw new Exception("未知异常：" + ex.ToString());
            }
        }
    }
}
